import sys
from graphviz import Graph

def p(outname='default'):
    g=Graph('G',filename=outname,engine='sfdp',node_attr={'style': 'filled'})
    g.attr('node',shape='ellipse')
    nodenum=int(input())
    for i in range(nodenum):
        na,colr=input().split(' ')
        g.node(na,color=colr)
    linenum=int(input())
    for i in range(linenum):
        a,b,c=input().split(' ')
        g.edge(a,b,color=c)
    g.view()
    

if __name__=='__main__':
    argv=sys.argv
    if len(argv)==2:
        p(argv[1])
    else:
        p()
    print("done")
